package com.zut.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zut.dao.OrderSettingDao;
import com.zut.pojo.OrderSetting;
import com.zut.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 预约服务设置
 * @author sq
 * @create 2021-07-01-15:30
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    //批量添加预约信息
    public void add(List<OrderSetting> list) {
        if(list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                //判断当前日期是否已经进行了预约设置
                long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if(countByOrderDate > 0){
                    //已经进行过预约设置，执行更新操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);

                }else{
                    //没有进行过预约设置，执行添加预约信息操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    //根据月份查询预约设置
    public List<Map> getOrderSettingByMonth(String date) {
        //设置日期查询开始条件，2021-7-1
        String begin = date + "-1";
        //设置日期查询结束条件，2021-7-31
        String end = date + "-31";

        //将查询条件添加到map集合中
        Map<String,String> dateMap = new HashMap<>();
        dateMap.put("begin",begin);
        dateMap.put("end",end);

        //调用Dao，根据日期范围查询预约设置数据
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(dateMap);

        //创建一个map类型的list，用于数据转换
        List<Map> result = new ArrayList<>();

        //遍历查询得到的OrderSetting预约实体类
        if(orderSettingList != null && orderSettingList.size() > 0){
            for (OrderSetting orderSetting : orderSettingList) {
                Map<String,Integer> map = new HashMap<>();
                //获取日期
                Date orderDate = orderSetting.getOrderDate();
                //获取日期的几号，并转换成int类型
                int day = orderDate.getDate();
                //获取可预约人数
                int number = orderSetting.getNumber();
                //获取已预约人数
                int reservations = orderSetting.getReservations();
                map.put("date",day);
                map.put("number",number);
                map.put("reservations",reservations);
                //将封装好的数据添加到返回结果的list集合中
                result.add(map);
            }
        }
        return result;
    }

    //根据日期编辑可预约人数
    public void editNumberByDate(OrderSetting orderSetting) {
        //获取数据库预约表中的日期是当前日期的记录数，大于0，代表设置过，小于0代表没有设置过
        long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        //判断当前日期是否已经进行了预约设置
        if(countByOrderDate > 0){
            //已经进行过预约设置，执行预约信息操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //没有进行过预约设置，执行添加预约信息操作
            orderSettingDao.add(orderSetting);
        }
    }
}
